#include "logger.hpp"
#include "util.hpp"
#include "db.hpp"
#include "online.hpp"
#include "server.hpp"
#define HOST "127.0.0.1"
#define PORT 3306
#define USER "root"
#define PASS "suqi181215"
#define DBNAME "gobang"

void Log_test()
{
    ILOG("Hello Linux, INF");
    DLOG("Hello Linux, DEBUG");
    ELOG("Hello Linux, ERROR");
}

void json_test()
{
    Json::Value root;
    std::string body;
    root["姓名"] = "小明";
    root["年龄"] = 18;
    root["成绩"].append(98);
    root["成绩"].append(88.5);
    root["成绩"].append(79);
    json_util::serialize(root, body);
    DLOG("%s", body.c_str());

    Json::Value val;
    json_util::unserialize(body, val);
    std::cout << "姓名: " << val["姓名"].asString() << std::endl;
    std::cout << "年龄: " << val["年龄"].asInt() << std::endl;
    int sz = val["成绩"].size();
    for (int i = 0; i < sz; i++)
    {
        std::cout << "成绩: " << val["成绩"][i].asFloat() << std::endl;
    }
}

void str_test()
{
    std::string str = "123,234,,,345";
    std::vector<std::string> array;
    string_util::split(str, ",", array);
    for (auto s : array)
    {
        DLOG("%s", s.c_str());
    }
}

void file_test()
{
    std::string filename = "./makefile";
    std::string body;
    file_util::read(filename, body);

    std::cout << body << std::endl;
}

void db_test()
{
    user_table ut(HOST, USER, PASS, DBNAME, PORT);
    Json::Value user;
    // 添加用户
    //  user["username"] = "FlashBack";
    //  user["password"] = "210322";
    //  bool ret = ut.insert(user);
    // 用户登录
    //  bool ret = ut.login(user);
    //  if (ret == false)
    //  {
    //      DLOG("LOGIN FAILED.");
    //  }
    // 通过用户名查找
    //  bool ret = ut.select_by_name("FlashBack", user);
    //  std::string body;
    //  json_util::serialize(user, body);
    //  std::cout << body << std::endl;
    // 通过id查找
    //  bool ret = ut.select_by_id(1, user);
    //  std::string body;
    //  json_util::serialize(user, body);
    //  std::cout << body << std::endl;
    // win
    //  bool ret = ut.win(1);
    //  ret = ut.win(1);
    // lose
    bool ret = ut.lose(1);
}

void online_test()
{
    online_manager om;
    wsserver_t::connection_ptr conn;
    uint64_t uid = 2;
    om.enter_game_room(uid, conn);
    if (om.is_in_game_room(uid))
    {
        DLOG("IN GAME HALL.");
    }
    else
    {
        DLOG("NOT IN GAME HALL.");
    }
    om.exit_game_room(uid);
    if (om.is_in_game_room(uid))
    {
        DLOG("IN GAME HALL.");
    }
    else
    {
        DLOG("NOT IN GAME HALL.");
    }
}

int main()
{
    gobang_server _server(HOST, USER, PASS, DBNAME, PORT);
    _server.start(8085);
    return 0;
}